package com.paypalpay.controller.inside;

import com.paypalpay.entity.common.Partner;
import com.paypalpay.pojo.Constant;
import com.paypalpay.pojo.request.RequestBody.SynchronousPartnerRequestBody;
import com.paypalpay.pojo.request.subRequest.SynchronousPartnerRequest;
import com.paypalpay.pojo.response.subResponse.SynchronousPartnerResponse;
import com.paypalpay.pojo.response.subResponse.common._Partner;
import com.paypalpay.service.PartnerService;
import com.paypalpay.turntointerfaceservice.utils.TurntoInterfaceUtils;
import com.paypalpay.util.JaxbUtil;
import com.paypalpay.util.wx.HttpClientTool;
import com.paypalpay.util.wx.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
//localhost:8081/paypalpay/syncPartner
@RestController
public class SynchronousPartnerController {
    @Autowired
    PartnerService partnerService;
    /**
     * 获取渠道列表
     *
     * @param body 请求体  凝趣 测试渠道编码 P000512
     * @return
     */
    @RequestMapping(value = "syncPartner", method = RequestMethod.GET)
    @ResponseBody
    public void synchronousPartner(SynchronousPartnerRequestBody body) {
        // 2,生成当前时间戳
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curtime = df.format(new Date());
        // 请求参数拼接
        SynchronousPartnerRequest request = new SynchronousPartnerRequest();
        TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
        com.paypalpay.pojo.request.RequestHeader header = utils.getRequestHeader("queryPartner", "GMSYS");
        request.setHeader(header);
        request.setBody(body);
        //String sign = utils.getSign(header, body, body.getClass(),"mlfcVrCbIQe06lEA1g4Fu6Ikfcf4jlMp");
        request.setSign("FFFFFFFFFFFFFFFFFFFFFFFFFFFF=");
        // 将参数转换成XML格式
        String sRequest = new JaxbUtil(SynchronousPartnerRequest.class).toXml(request, "utf-8");
        System.out.println("请求值：" + sRequest);
        // 发送post请求，并接收返回结果
        String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
        System.out.println("返回结果：" + result);
        // 将回传的xml转换成object数据
        SynchronousPartnerResponse responseObj = new JaxbUtil(SynchronousPartnerResponse.class).fromXml(result, "utf-8");

        // 返回错误结果的处理
        String retCode = responseObj.getHeader().getResultCode();

        if (true == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
            System.out.println("返回结果：" + retCode);
            List<_Partner> partnerList = responseObj.getBody().getPartner();
            for (_Partner p : partnerList) {
                String partnerName = p.getPartnerName();
                String partnerCode = p.getPartnerCode();
                String appScope = p.getAppScope();
                String secret = p.getSecret();
                String token = MD5.calcMD5(partnerName + partnerCode + appScope + secret);

                //先去数据库查询partnerCode是不是已经存在
                int count = partnerService.findPartnerCodeEixtOrNot(partnerCode);

                if (count <= 0) {
                    //入库
                    Partner partner = new Partner();
                    partner.setPartnerName(partnerName);
                    partner.setPartnerCode(partnerCode);
                    partner.setAppScope(appScope);
                    partner.setSecret(secret);
                    partner.setToken(token);
                    partnerService.insertPartner(partner);

                }
            }
            System.out.println("同步资费套餐结果成功：" + retCode);
        } else {
            System.out.println("同步资费套餐结果成功：" + retCode);
        }

    }

}
